package model.dap.dao;

import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;

import common.base.DaoConfig;
import model.dap.domain.Consumer;
import model.dap.mapper.ConsumerMapper;

public class ConsumerDAO implements ConsumerMapper {

	SqlSession session = DaoConfig.getSqlSessionFactory().openSession();
	Logger log = Logger.getLogger(ConsumerDAO.class);
	// =====================================================================================================
	// LOGIN CONSUMER
	@Override
	public Consumer login(Consumer mastUser) {
		// TODO Auto-generated method stub
		try {
			return session.getMapper(ConsumerMapper.class).login(mastUser);
		} catch (Exception e) {
			log.error(e.getMessage());
			session.rollback();
		} finally {
			session.commit();
			session.close();
		}
		return null;
	}
	
	@Override
	public Consumer login2(Consumer mastUser) {
		// TODO Auto-generated method stub
		try {
			session = DaoConfig.getSqlSessionFactory().openSession();
			return session.getMapper(ConsumerMapper.class).login(mastUser);
		} catch (Exception e) {
			log.error(e.getMessage());
			session.rollback();
		} finally {
			session.commit();
			session.close();
		}
		return null;
	}
	
	// =====================================================================================================
	// REGISTER CONSUMER
	@Override
	public int insertConsumer(Consumer mastUser) {
		// TODO Auto-generated method stub
		try {
			return session.getMapper(ConsumerMapper.class).insertConsumer(
					mastUser);
		} catch (Exception e) {
			log.error(e.getMessage());
			session.rollback();
		} finally {
			session.commit();
			session.close();
		}
		return 0;
	}
	// =====================================================================================================
	// VALIDATE CONSUMER EXIST
	@Override
	public Consumer validateConsumerExist(String userID) {
		// TODO Auto-generated method stub
		try {
			return session.getMapper(ConsumerMapper.class)
					.validateConsumerExist(userID);
		} catch (Exception e) {
			session.rollback();
		} finally {
			session.close();
		}
		return null;
	}
	
	// =====================================================================================================
	// VALIDATE CONSUMER EMAIL EXIST
	@Override
	public Consumer validateConsumerEmailExist(String email) {
		// TODO Auto-generated method stub
		try {
			return session.getMapper(ConsumerMapper.class)
					.validateConsumerEmailExist(email);
		} catch (Exception e) {
			session.rollback();
		} finally {
			session.close();
		}
		return null;
	}
	
	// =====================================================================================================
	// GET CONSUMER BY ID
	@Override
	public Consumer getConsumerbyId(String userID) {
		// TODO Auto-generated method stub
		SqlSession session = DaoConfig.getSqlSessionFactory().openSession();
		try {
			return session.getMapper(ConsumerMapper.class).getConsumerbyId(
					userID);
		} catch (Exception e) {
			// TODO: handle exception
			session.rollback();
		} finally {
			session.close();
		}

		return null;
	}
	
	// =====================================================================================================
	// GET CONSUMER BY ID 2
		@Override
		public Consumer getConsumerbyId2(String userID) {
			// TODO Auto-generated method stub
			SqlSession session = DaoConfig.getSqlSessionFactory().openSession();
			try {
				return session.getMapper(ConsumerMapper.class).getConsumerbyId2(
						userID);
			} catch (Exception e) {
				// TODO: handle exception
				session.rollback();
			} finally {
				session.close();
			}

			return null;
		}
	
	// =====================================================================================================
	// VALIDATE CONSUMER OR NOT
	@Override
	public Consumer validateConsumerOrNot(String userID) {
		// TODO Auto-generated method stub
		try {
			return session.getMapper(ConsumerMapper.class)
					.validateConsumerOrNot(userID);
		} catch (Exception e) {
			session.rollback();
		} finally {
			session.close();
		}
		return null;
	}
	
	
	// =====================================================================================================
	// VALIDATE CONSUMER STATUS
	@Override
	public Consumer validateConsumerStatus(String userID) {
		// TODO Auto-generated method stub
		try {
			return session.getMapper(ConsumerMapper.class)
					.validateConsumerStatus(userID);
		} catch (Exception e) {
			session.rollback();
		} finally {
			session.close();
		}
		return null;
	}
	
	// =====================================================================================================
	// VALIDATE CONSUMER ACTIVE OR NOT
	@Override
	public Consumer validateActiveConsumer(String userID) {
		// TODO Auto-generated method stub
		try {
			return session.getMapper(ConsumerMapper.class)
					.validateActiveConsumer(userID);
		} catch (Exception e) {
			session.rollback();
		} finally {
			session.close();
		}
		return null;
	}
	
	// =====================================================================================================
	// VALIDATE OLD PASSWORD IS MATCH WITH CONSUMER
	@Override
	public Consumer validateOldPassword(Consumer mastUser) {
		// TODO Auto-generated method stub
		try {
			return session.getMapper(ConsumerMapper.class).validateOldPassword(
					mastUser);
		} catch (Exception e) {
			log.error(e.getMessage());
			session.rollback();
		} finally {
			session.commit();
			session.close();
		}
		return null;
	}
	
	// =====================================================================================================
	// UPDATE CONSUMER PROFILE
	@Override
	public int updateConsumer(Consumer mastUser) {
		// TODO Auto-generated method stub
		try {
			return session.getMapper(ConsumerMapper.class).updateConsumer(
					mastUser);
		} catch (Exception e) {
			// TODO: handle exception
			log.error(e.getMessage());
			session.rollback();
		} finally {
			session.commit();
			session.close();
		}
		return 0;
	}
	
	@Override
	public Consumer retrievePassword(Consumer mastUser) {
		// TODO Auto-generated method stub
		try {
			return session.getMapper(ConsumerMapper.class).retrievePassword(mastUser);
		} catch (Exception e) {
			// TODO: handle exception
			log.error(e.getMessage());
			session.rollback();
		} finally{
			session.commit();
			session.close();
		}
		
		return null;
	}

	@Override
	public int updateTemporaryPassword(Consumer mastUser) {
		// TODO Auto-generated method stub
		try {
			return session.getMapper(ConsumerMapper.class).updateTemporaryPassword(mastUser);
		} catch (Exception e) {
			// TODO: handle exception
			log.error(e.getMessage());
			session.rollback();
		} finally {
			session.commit();
			session.close();
		}
		
		return 0;
	}

	@Override
	public Consumer validateTemporaryPassword(String temporaryPassword) {
		// TODO Auto-generated method stub
		try {
			return session.getMapper(ConsumerMapper.class).validateTemporaryPassword(temporaryPassword);
		} catch (Exception e) {
			// TODO: handle exception
			log.error(e.getMessage());
			session.rollback();
		} finally {
			session.commit();
			session.close();
		}
		
		return null;
	}

	// =====================================================================================================
	// UPDATE PASSWORD
	@Override
	public int updatePassword(Consumer mastUser) {
		// TODO Auto-generated method stub
		SqlSession session = DaoConfig.getSqlSessionFactory().openSession();
		try {
			return session.getMapper(ConsumerMapper.class).updatePassword(
					mastUser);
		} catch (Exception e) {
			log.error(e.getMessage());
			session.rollback();
		} finally {
			session.commit();
			session.close();
		}

		return 0;
	}

	@Override
	public Consumer validateEmailMatchUserID(Consumer mastUser) {
		// TODO Auto-generated method stub
		SqlSession session = DaoConfig.getSqlSessionFactory().openSession();
		try {
			return session.getMapper(ConsumerMapper.class).validateEmailMatchUserID(mastUser);
		} catch (Exception e) {
			log.error(e.getMessage());
			session.rollback();
		} finally {
			session.commit();
			session.close();
		}
		return null;
	}

}
